<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Route,URL,Auth;
use Log;


class IndexController extends Controller
{
    public function index()
    {	







    	if ($_POST) {
    		$narid = $_POST['id'];
    		$aid['id'] = $_POST['id'];
    	}else{
    		$narid = 1;
    		$aid['id'] = 1;
    	}


    	$area = DB::table('area')->where($aid)->first();
    		if ($area->dengji == 1) {

			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['city', '=', $narid],
			    ['is_imp', '<>', '0'],
			])->orderBy('id','desc')->get();

			//获取重要部位
			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.city = $narid) GROUP BY zy.id,zy.name,gname");

			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.city = $narid) GROUP BY zr.id,zr.name,gname");


			//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.city = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");


		}

		if ($area->dengji == 2) {
			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['area', '=', $narid],
			    ['is_imp', '<>', '0'],
			])->orderBy('id','desc')->get();

			//获取重要部位
			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.area = $narid) GROUP BY zy.id,zy.name,gname");

			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.area = $narid) GROUP BY zr.id,zr.name,gname");

		


    		//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.area = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");
		}

		if ($area->dengji == 3) {
			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['org_id', '=', $narid],
			    ['is_imp', '<>', '0'],
			])->orderBy('id','desc')->get();

			
			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.org_id = $narid) GROUP BY zy.id,zy.name,gname");


			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.org_id = $narid) GROUP BY zr.id,zr.name,gname");

			
			//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.org_id = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");

		}

		
		//获取登录人的id
		$userid['id'] = Auth::guard('admin')->user()->unit_id;
		$unittype = DB::table('units')->where($userid)->select('type')->first();

        return view('admin.index.index',['res'=>$re,'zybw'=>$zybw,'zdry'=>$zdry,'unittype'=>$unittype]);
    }

	public function home(){
		return view('admin.index.home');
	}
    
}
